package com.example.service.impl;

import com.example.mapper.LoginMapper;
import com.example.pojo.LoginMsg;
import com.example.service.LoginService;
import com.example.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class LoginImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;
    @Override
    public LoginMsg login(LoginMsg loginmsg) {
        LoginMsg login;
        log.info("登录请求参数：{}", loginmsg);
        if (loginmsg.getRole()==1) {
            login= loginMapper.studentLogin(loginmsg);
            if (login!=null){
                login.setRole(1);
            }
        }else if (loginmsg.getRole()==2) {
            login= loginMapper.teacherLogin(loginmsg);
            if (login!=null){
                login.setRole(2);
            }
        }else {
            login = loginMapper.adminLogin(loginmsg);
            if (login!=null){
                login.setRole(3);
            }
        }
        log.info("登录请求结果：{}", login);
        if(login!=null){
            Map<String,Object> claims = new HashMap<>();
            claims.put("username",login.getUsername());
            claims.put("role",login.getRole());
            login.setToken(JwtUtils.generateToken(claims));
        }
        log.info("登录请求结果：{}", login);
          return login;
    }
}
